package org.dromara.bzIns.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bzIns.domain.bo.InsCompanyBo;
import org.dromara.bzIns.domain.vo.InsCompanyVo;
import org.dromara.bzIns.domain.InsCompany;
import org.dromara.bzIns.mapper.InsCompanyMapper;
import org.dromara.bzIns.service.IInsCompanyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保险公司信息Service业务层处理
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class InsCompanyServiceImpl implements IInsCompanyService {

    private final InsCompanyMapper baseMapper;

    /**
     * 查询保险公司信息
     *
     * @param insCompanyId 主键
     * @return 保险公司信息
     */
    @Override
    public InsCompanyVo queryById(Long insCompanyId){
        return baseMapper.selectVoById(insCompanyId);
    }

    /**
     * 分页查询保险公司信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保险公司信息分页列表
     */
    @Override
    public TableDataInfo<InsCompanyVo> queryPageList(InsCompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InsCompany> lqw = buildQueryWrapper(bo);
        Page<InsCompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保险公司信息列表
     *
     * @param bo 查询条件
     * @return 保险公司信息列表
     */
    @Override
    public List<InsCompanyVo> queryList(InsCompanyBo bo) {
        LambdaQueryWrapper<InsCompany> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InsCompany> buildQueryWrapper(InsCompanyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsCompany> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(InsCompany::getInsCompanyId);
        lqw.like(StringUtils.isNotBlank(bo.getInsCompanyName()), InsCompany::getInsCompanyName, bo.getInsCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getInsClaimPhone()), InsCompany::getInsClaimPhone, bo.getInsClaimPhone());
        lqw.eq(bo.getIsDisplay() != null, InsCompany::getIsDisplay, bo.getIsDisplay());
        return lqw;
    }

    /**
     * 新增保险公司信息
     *
     * @param bo 保险公司信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InsCompanyBo bo) {
        InsCompany add = MapstructUtils.convert(bo, InsCompany.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInsCompanyId(add.getInsCompanyId());
        }
        return flag;
    }

    /**
     * 修改保险公司信息
     *
     * @param bo 保险公司信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InsCompanyBo bo) {
        InsCompany update = MapstructUtils.convert(bo, InsCompany.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InsCompany entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保险公司信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
